00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef _nonlinear_solver_implementation_hpp_
00022 #define _nonlinear_solver_implementation_hpp_
00023
00024 #include <boost/shared_ptr.hpp>
00025 #include <gridpack/utilities/complex.hpp>
00026 #include <gridpack/math/nonlinear_solver_functions.hpp>
00027 #include <gridpack/math/nonlinear_solver_interface.hpp>
00028 #include <gridpack/parallel/distributed.hpp>
00029 #include <gridpack/utilities/uncopyable.hpp>
00030 #include <gridpack/configuration/configurable.hpp>
00031
00032 namespace gridpack {
00033 namespace math {
00034
00035
00036
00037
00038 template <typename T, typename I = int>
00039 class NonlinearSolverImplementation
00040 : public NonlinearSolverInterface<T, I>,
00041 public parallel::Distributed,
00042 public utility::Configurable,
00043 private utility::Uncopyable
00044 {
00045 public:
00046
00047 typedef typename NonlinearSolverInterface<T, I>::VectorType VectorType;
00048 typedef typename NonlinearSolverInterface<T, I>::MatrixType MatrixType;
00049 typedef typename NLSBuilder<T, I>::Jacobian JacobianBuilder;
00050 typedef typename NLSBuilder<T, I>::Function FunctionBuilder;
00051
00052
00053 struct null_deleter
00054 {
00055 void operator()(void const *) const { }
00056 };
00057
00058
00059 NonlinearSolverImplementation(const parallel::Communicator& comm,
00060 const int& local_size,
00061 JacobianBuilder form_jacobian,
00062 FunctionBuilder form_function)
00063 : NonlinearSolverInterface<T, I>(),
00064 parallel::Distributed(comm),
00065 utility::Configurable("NonlinearSolver"),
00066 utility::Uncopyable(),
00067 p_J(), p_F(),
00068 p_X((VectorType *)NULL, null_deleter()),
00069 p_jacobian(form_jacobian),
00070 p_function(form_function),
00071 p_solutionTolerance(1.0e-05),
00072 p_functionTolerance(1.0e-10),
00073 p_maxIterations(50)
00074 {
00075 p_F.reset(new VectorType(this->communicator(), local_size));
00076
00077
00078
00079
00080 p_J.reset(new MatrixType(this->communicator(), local_size, local_size, Sparse));
00081 }
00082
00083
00084 NonlinearSolverImplementation(MatrixType& J,
00085 JacobianBuilder form_jacobian,
00086 FunctionBuilder form_function)
00087 : NonlinearSolverInterface<T, I>(),
00088 parallel::Distributed(J.communicator()),
00089 utility::Configurable("NonlinearSolver"),
00090 utility::Uncopyable(),
00091 p_J(&J, null_deleter()), p_F(),
00092 p_X((VectorType *)NULL, null_deleter()),
00093 p_jacobian(form_jacobian),
00094 p_function(form_function),
00095 p_solutionTolerance(1.0e-05),
00096 p_functionTolerance(1.0e-10),
00097 p_maxIterations(50)
00098 {
00099 p_F.reset(new VectorType(this->communicator(), J.localRows()));
00100 }
00101
00102
00103
00104 virtual ~NonlinearSolverImplementation(void)
00105 {
00106
00107 }
00108
00109
00110 protected:
00111
00112
00113 boost::shared_ptr<MatrixType> p_J;
00114
00115
00116 boost::shared_ptr<VectorType> p_F;
00117
00118
00119 boost::shared_ptr<VectorType> p_X;
00120
00121
00122 JacobianBuilder p_jacobian;
00123
00124
00125 FunctionBuilder p_function;
00126
00127
00128 double p_solutionTolerance;
00129
00130
00131 double p_functionTolerance;
00132
00133
00134 int p_maxIterations;
00135
00136
00137 double p_tolerance(void) const
00138 {
00139 return p_solutionTolerance;
00140 }
00141
00142
00143 void p_tolerance(const double& tol)
00144 {
00145 p_solutionTolerance = tol;
00146 }
00147
00148
00149 int p_maximumIterations(void) const
00150 {
00151 return p_maxIterations;
00152 }
00153
00154
00155 void p_maximumIterations(const int& n)
00156 {
00157 p_maxIterations = n;
00158 }
00159
00160
00161 void p_solve(VectorType& x)
00162 {
00163
00164 p_X.reset(&x, null_deleter());
00165 }
00166
00167
00168 void p_configure(utility::Configuration::CursorPtr props)
00169 {
00170 if (props) {
00171 p_solutionTolerance = props->get("SolutionTolerance", p_solutionTolerance);
00172 p_functionTolerance = props->get("FunctionTolerance", p_functionTolerance);
00173 p_maxIterations = props->get("MaxIterations", p_maxIterations);
00174 }
00175 }
00176
00177 };
00178
00179
00180 }
00181 }
00182
00183
00184 #endif